###########################################################################################
all: ${ALL}

${ALL}:
	${MAKE} check NAME=$@

NAME 		?=package
MAKE_ARGS 	?=-j3

PATH:=${BUILDDIR}/bin:${PATH}

check: ${BUILDDIR}/${NAME}.install

${BUILDDIR}/${NAME}.install: distfiles/$(${NAME}_tarball)
	@( \
		( \
			mkdir -p ${NAME}.work && \
			case "$?" in \
				*.gz | *.tgz) tar zxf "distfiles/$(${NAME}_tarball)" -C "${NAME}.work";; \
				*.bz2 | *.tbz) tar jxf "distfiles/$(${NAME}_tarball)" -C "${NAME}.work";; \
				*) echo "$?: Unknown type of archive"; exit 1;; \
			esac && \
			( \
				cd ${NAME}.work/$(${NAME}_tarball_dir) \
				&& ./configure $(${NAME}_options) --prefix="${BUILDDIR}" \
				&& gmake -j6 all CC="${CC}" CFLAGS="$(${NAME}_cflags)"\
				&& gmake install \
				&& ( \
					cd ${BUILDDIR}; \
					echo -n > ${NAME}.install; \
					find . -type f | sort > /tmp/dir; \
					{ cat *.install 2>/dev/null || echo; } | sort | comm -13 - /tmp/dir > ${BUILDDIR}/${NAME}.install \
				) \
				&& ( cd ${BUILDDIR}; $(${NAME}_post_install) ) \
			) \
			&& rm -rf ${NAME}.work \
		) \
	)

uninstall:
	( \
		cd ${BUILDDIR} && \
		rm -f `cat ${NAME}.install` && \
		find . -type d -empty -delete \
	)

${NAME}.work/$(${NAME}_tarball_dir)/${NAME}.dir: distfiles/$(${NAME}_tarball)
	@( \
		mkdir -p ${NAME}.work && \
		case "$?" in \
			*.gz | *.tgz) tar zxf "distfiles/$(${NAME}_tarball)" -C "${NAME}.work";; \
			*.bz2 | *.tbz) tar jxf "distfiles/$(${NAME}_tarball)" -C "${NAME}.work";; \
			*) echo "$?: Unknown type of archive"; exit 1;; \
		esac \
	)


help: ${NAME}.work/$(${NAME}_tarball_dir)/${NAME}.dir
	( { cd `dirname $?` && ./configure --help | { \
		sed -n -r '1h;1!H;$${g;s/\n {20,}/ /g;p;}' | sort \
		| grep -v -E '\-\-(help|no\-create|quiet|version|target|with\-x)[ =,]' \
		| grep -E '^  \-\-' \
		| sed -r 's/^  //' \
	;};} | { pat=`echo "$(${NAME}_options)" | sed -r "s/(=[^=]*)?[ \t]+/\|/g"`; sed -r "s/^$$pat[ ,=]/\x1B[1;31m\0\x1B[0m/g"; } )

clean:
	rm -rf *.work

distclean: clean
	if [ -d "${BUILDDIR}" ]; then cd ${BUILDDIR}/.. && rm -rf .build; fi




